//
//  CommentViewController.m
//  1243
//
//  Created by 王 on 16/5/26.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "CommentViewController.h"
#import "CommentCell.h"

@interface CommentViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *array;

@end

@implementation CommentViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStyleGrouped];
    }
    return _tableView;
}

- (NSArray *)array {
    if (!_array) {
        _array = @[@"falfjalkfjakljfklagan沙发房间按房间爱老房间爱老师开房间爱离开房间爱快乐十分健康了", @"给大力加快放假啦", @"撒娇放假安吉拉卡斯加罚款了十分骄傲了开始放家里卡收费记录卡飞机安康你家开了房间爱里的那几款理财那就看你擦了擦你来的呢你出来的刹那间sfajfkafjljl 加上了手机爱离开十分分散法大大大大啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊是是是是啊\n啊啊啊啊啊啊啊啊啊啊试试事实上是是是是是是是是是罚款了罚款了发了疯就考虑是否就是坑垃圾分类考试解放路口的看到了", @"哈哈"];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.view.backgroundColor = [UIColor blueColor];
    
//    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Identifier];
    }
    
    cell.textLabel.text = @"吉萨德去";
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.text = @"2016-05-20";
    cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
    cell.imageView.image = [UIImage imageNamed:@"touxiang"];
    cell.imageView.backgroundColor = [UIColor redColor];
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 13;
    
    if (indexPath.row == 1) {
        CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CommentCell" owner:self options:nil] lastObject];
        }
//        cell.commentLabel.text = self.array[indexPath.section];
        [cell cellAutoLayoutHeight:self.array[indexPath.section]];
        return cell;
    }
    
    return cell;
}

#pragma mark - UITableView DataSource

//去掉cell距离左边的间距
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsMake(0, 16, 0, 0);
    cell.preservesSuperviewLayoutMargins = NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

//在 heightForRow 中实现 cell 自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CommentCell" owner:self options:nil] lastObject];
        }
        [cell cellAutoLayoutHeight:self.array[indexPath.section]];
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
        return size.height + 30;
    } else {
        return 50;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
